﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

namespace INFTUR
{
    public partial class WebForm1 : System.Web.UI.Page


    {
        public static bool inserir = false;
        
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void Limpar()
        {
            TbID.Text = "";
            TbID.Enabled = false;
            Tbnome.Text = "";
            Tbnome.Enabled = false;
            Tbtelefone.Text = "";
            Tbtelefone.Enabled = false;
            Tbsite.Text = "";
            Tbsite.Enabled = false;
            Tbendereco.Text = "";
            Tbendereco.Enabled = false;
            Tbemail.Text = "";
            Tbemail.Enabled = false;
            DDLestrelas.SelectedValue = "1";
            DDLestrelas.Enabled = false;

            BBcancelar.Enabled = false;
            BBgravar.Enabled = false;
        }

        protected void BBgravar_Click(object sender, EventArgs e)
        {
            try
            {

                HotelBO hBO = new HotelBO();
                hBO.Nome = Tbnome.Text;
                hBO.Site = Tbsite.Text;
                hBO.Telefone = Tbtelefone.Text;
                hBO.Endereco = Tbendereco.Text;
                hBO.Email = Tbemail.Text;
                hBO.Estrelas = Convert.ToInt32(DDLestrelas.SelectedValue);

                HotelBOMetodos hBOmetodos = new HotelBOMetodos();
                if (inserir)
                    hBOmetodos.inserir(hBO);
                else
                {
                    hBO.Id = Convert.ToInt32(TbID.Text);
                    hBOmetodos.atualizar(hBO);
                }

                Limpar();
            }
            catch (Exception Ex){
                Lberro.Text = Ex.Message;

            }
            GridView1.DataBind();
        }

        protected void BBcancelar_Click(object sender, EventArgs e)
        {
            Limpar();
        }

        protected void BBnovo_Click(object sender, EventArgs e)
        {
            Tbnome.Enabled = true;
            Tbsite.Enabled = true;
            Tbtelefone.Enabled = true;
            Tbendereco.Enabled = true;
            Tbemail.Enabled = true;
            DDLestrelas.Enabled = true;

            BBgravar.Enabled = true;
            BBcancelar.Enabled = true;

            inserir = true;
        }

        protected void LbAtualizar_Click(object sender, EventArgs e)
        {
            LinkButton lb =(LinkButton)sender;
            DataControlFieldCell celula = (DataControlFieldCell)lb.Parent;
            GridViewRow Linha = (GridViewRow)celula.Parent;
            int ID = Convert.ToInt32(Linha.Cells[0].Text);
            
            Tbnome.Enabled = true;
            Tbsite.Enabled = true;
            Tbtelefone.Enabled = true;
            Tbendereco.Enabled = true;
            Tbemail.Enabled = true;
            DDLestrelas.Enabled = true;
            BBgravar.Enabled = true;
            BBcancelar.Enabled = true;

            inserir = true;
        
            HotelBO HB = new HotelBO();
            HotelBOMetodos HBM = new HotelBOMetodos();
            HB = HBM.Consultar(ID);
            TbID.Text = HB.Id.ToString();
            Tbnome.Text = HB.Nome;
            Tbsite.Text = HB.Site;
            Tbtelefone.Text = HB.Telefone;
            Tbendereco.Text = HB.Endereco;
            Tbemail.Text = HB.Email;
            DDLestrelas.SelectedValue = HB.Estrelas.ToString();
            GridView1.DataBind();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
        LinkButton lb =(LinkButton)sender;
            DataControlFieldCell celula = (DataControlFieldCell)lb.Parent;
            GridViewRow Linha = (GridViewRow)celula.Parent;
            int ID = Convert.ToInt32(Linha.Cells[0].Text);
        
            HotelBO HB = new HotelBO();
            HotelBOMetodos HBM = new HotelBOMetodos();
            HBM.remover(ID);
            GridView1.DataBind();
        }

        protected void ObsHotel_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {

        }

       
        }
      
        


    }

